package com.jvyou.framework.controller;



import com.jvyou.common.core.domain.entity.User;
import com.jvyou.framework.domain.dto.UserDto;
import com.jvyou.framework.service.UserService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import java.util.List;
import com.jvyou.common.annotation.Log;
import com.jvyou.common.core.domain.model.PageResult;
import com.jvyou.common.core.domain.model.Result;
import com.jvyou.common.enums.log.BusinessType;
import com.jvyou.common.enums.result.HttpCodeEnum;


/**
 * 用户表(User)表控制层
 *
 * @author 橘柚
 * @since 2025-05-29 17:20:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController  {
    
    private final UserService userService;
    
     /**
     * 查询用户表列表
     *
     * @param dto 查询参数
     * @return 用户表
     */
    @GetMapping("/list")
    @SaCheckPermission("sys:user:list")
    @Log(title = "查询用户表列表", businessType = BusinessType.OTHER)
    public Result<List<User>> list(@ModelAttribute UserDto dto) {
        return Result.success(userService.listUser(dto));
    }

     /**
     * 分页查询用户表列表
     *
     * @param dto 查询参数
     * @return 用户表
     */
    @GetMapping("/page")
    @SaCheckPermission("sys:user:page")
    @Log(title = "分页查询用户表列表", businessType = BusinessType.OTHER)
    public Result<PageResult<User>> page(@ModelAttribute UserDto dto) {
        return Result.success(userService.pageUser(dto));
    }

    /**
     * 通过主键查询用户表单条
     *
     * @param id 用户表主键
     * @return 用户表
     */
    @GetMapping("/{id}")
    @Log(title = "通过主键查询用户表")
    @SaCheckPermission("sys:user:get")
    public Result<User> selectOne(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 新增用户表
     *
     * @param user 用户表实体对象
     * @return 新增用户表结果
     */
    @PostMapping
    @Log(title = "新增用户表",  businessType = BusinessType.INSERT)
    @SaCheckPermission("sys:user:insert")
    public Result<User> insert(@RequestBody User user) {
        boolean success = userService.save(user);
        return success ? Result.success(user) : Result.error(HttpCodeEnum.ADD_FAIL);
    }
    
     /**
     * 修改用户表
     *
     * @param user 用户表实体对象
     * @return 修改用户表结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("sys:user:update")
    @Log(title = "修改用户表数据", businessType = BusinessType.UPDATE)
    public Result<User> updateById(@RequestBody User user) {
        boolean success = userService.updateById(user);
        return success ? Result.success(user) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }
    

    /**
     * 删除用户表数据
     *
     * @param id 用户表主键
     * @return 删除用户表结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("sys:user:delete")
    @Log(title = "删除用户表",  businessType = BusinessType.DELETE)
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = userService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }
    
}

